package com.wzdigit.wms.wms.controller.TransferWareHouse;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.transfer.SearchTransferManageRequest;
import com.wzdigit.wms.basic.client.request.transfer.UpdateTransferWhsRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.transfer.TransferManageDtlGoodsResponse;
import com.wzdigit.wms.basic.client.response.transfer.TransferManageDtlResponse;
import com.wzdigit.wms.basic.client.response.transfer.TransferManageResponse;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.service.transfer.TransferManageService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 调拨单管理_PC 前端控制器
 * @author Administrator
 *
 */
@Slf4j
@RestController
@RequestMapping(value = WmsWareConstant.API_VERSION)
@Api(tags = "调拨单管理_PC")
public class TransferManageController {

    public static final String REQUEST_MODEL = "/transfermanage";

    @Autowired
    private TransferManageService transferManageService;

    /**
	 * 调拨单列表
	 * @param dto
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "调拨单列表",notes = "调拨单列表")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/searchTransferManage")
    public PagerInfo<List<TransferManageResponse>> searchTransferManage(
    		SearchTransferManageRequest dto, Pager<?> pager,
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return transferManageService.searchTransferManage(dto, pager);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
	/**
	 * 调拨单明细列表
	 * @param transferHeaderId
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "调拨单明细列表",notes = "调拨单明细列表")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/searchTransferManageDtl")
    public List<TransferManageDtlResponse> searchTransferManageDtl(
    		@ApiParam(value = "调拨单表头ID", required = true) 
    		@RequestParam(required = true) Integer transferHeaderId, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return transferManageService.searchTransferManageDtl(transferHeaderId);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
	/**
	 * 调拨单明细SN列表
	 * @param transferDtlId
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "调拨单明细SN列表",notes = "调拨单明细SN列表")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/searchTransferManageDtlGoods")
    public List<TransferManageDtlGoodsResponse> searchTransferManageDtlGoods(
    		@ApiParam(value = "调拨单表头ID", required = true) 
    		@RequestParam(required = true) Integer transferHeaderId, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return transferManageService.searchTransferManageDtlGoods(transferHeaderId);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
	/**
	 * 修改 调拨单【来源仓库、目标仓库】
	 * @param transferNo
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 * @throws Exception 
	 */
	@ApiOperation(value = "修改 调拨单【来源仓库、目标仓库】",notes = "修改 调拨单【来源仓库、目标仓库】")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PutMapping(value = REQUEST_MODEL + "/updateTransferWhs")
    public Result<?> updateTransferWhs(
    		@RequestBody @Valid UpdateTransferWhsRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) throws Exception {
        try {
            return transferManageService.updateTransferWhs(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
	/**
	 * 关单
	 * @param transferNo
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 * @throws Exception 
	 */
	@ApiOperation(value = "关单",notes = "关单")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PutMapping(value = REQUEST_MODEL + "/close")
    public Result<Boolean> close(
    		@ApiParam(value = "调拨单单号", required = true) 
    		@RequestParam(required = true) String transferNo, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) throws Exception {
        try {
            return transferManageService.close(transferNo, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
	/**
	 * 导出调拨单
	 * @param dto
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 * @throws Exception 
	 */
	@ApiOperation(value = "导出调拨单",notes = "导出调拨单")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/exportTransferReportList")
    public void exportTransferReportList(
    		SearchTransferManageRequest dto, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) throws Exception {
        try {
            transferManageService.exportTransferReportList(dto, servletRequest, servletResponse);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
}
